package com.niiwoo.civet.trade.configuration;

import com.niiwoo.civet.trade.constant.LoanFlowMqConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 借款流程相关MQ配置
 */
@Configuration
public class LoanFlowMqConfiguration {

    @Bean
    public Exchange loanFlow(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    // 借款相关数据回流
    @Bean
    public Queue loanDataBackflowQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.LOAN_DATA_BACK_FLOW_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding loanDataBackflowBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(loanDataBackflowQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.LOAN_DATA_BACK_FLOW_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 授权状态检测
    @Bean
    public Queue authStatusCheckQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.AUTH_STATUS_CHECK_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding authStatusCheckBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(authStatusCheckQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.AUTH_STATUS_CHECK_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 12项风险规则处理
    @Bean
    public Queue riskInfoHandleQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.RISK_INFO_HANDLE_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding riskInfoHandleBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(riskInfoHandleQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.RISK_INFO_HANDLE_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 借款反欺诈
    @Bean
    public Queue loanAntiFraudQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.LOAN_ANTI_FRAUD_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding loanAntiFraudBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(loanAntiFraudQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.LOAN_ANTI_FRAUD_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 额度评分卡
    @Bean
    public Queue loanLimitScoreQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.LOAN_LIMIT_SCORE_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding loanLimitScoreBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(loanLimitScoreQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.LOAN_LIMIT_SCORE_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 提额额度评分卡
    @Bean
    public Queue loanTeLimitScoreQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.LOAN_TE_LIMIT_SCORE_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding loanTeLimitScoreBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(loanTeLimitScoreQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.LOAN_TE_LIMIT_SCORE_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 系统审核
    @Bean
    public Queue loanSystemAuditQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.LOAN_SYSTEM_AUDIT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding loanSystemAuditBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(loanSystemAuditQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.LOAN_SYSTEM_AUDIT_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 肖像认证
    @Bean
    public Queue portraitAuthQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.PORTRAIT_AUTH_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding portraitAuthBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(portraitAuthQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.PORTRAIT_AUTH_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 人工审核
    @Bean
    public Queue manualAuditQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.MANUAL_AUDIT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding manualAuditBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(manualAuditQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.MANUAL_AUDIT_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    // 上标(自动审核开关开启)
    @Bean
    public Queue projectToBankQueue(RabbitAdmin admin) {
        Queue queue = new Queue(LoanFlowMqConstant.Queue.PROJECT_TO_BANK_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding projectToBankBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(projectToBankQueue(admin)).to(loanFlow(admin)).with(LoanFlowMqConstant.getRoutingKey(LoanFlowMqConstant.Exchange.LOAN_FLOW_EXCHANGE, LoanFlowMqConstant.Queue.PROJECT_TO_BANK_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

}
